package com.youtellv1.controller;

import com.github.pagehelper.PageInfo;
import com.youtellv1.config.ResultData;
import com.youtellv1.entity.Teachers;
import com.youtellv1.service.TeachersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Base64;

@RestController
@RequestMapping("/api/v1/manager/teacher")
public class TeachersController {
    @Autowired
    private TeachersService teachersService;

    @GetMapping("/list")
    public ResultData<PageInfo<Teachers>> list(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "6") int size) {
        try {
            PageInfo<Teachers> pageInfo = teachersService.findTeachersByPage(page, size);
            return new ResultData<>(pageInfo);
        } catch (Exception e) {
            return new ResultData<>(500, "分页获取失败：" + e.getMessage(), null);
        }
    }

    @PostMapping("/save-multipart")
    public ResultData<Void> saveTeachers(
            @RequestParam("tname") String tname,
            @RequestParam("tsex") String tsex,
            @RequestParam("tage") int tage,
            @RequestParam("ttype") int ttype,
            @RequestParam("tyear") int tyear,
            @RequestParam("tspec") String tspec,
            @RequestParam("tschool") String tschool,
            @RequestParam("tphone") String tphone,
            @RequestParam("tintr") String tintr,
            @RequestParam("flag") String flag,
            @RequestParam("file") MultipartFile file
    ) {
        try {
            Teachers teacher = new Teachers();
            teacher.setTname(tname);
            teacher.setTsex(tsex);
            teacher.setTage(tage);
            teacher.setTtype(ttype);
            teacher.setTyear(tyear);
            teacher.setTspec(tspec);
            teacher.setTschool(tschool);
            teacher.setTphone(tphone);
            teacher.setTintr(tintr);
            teacher.setFlag(flag);
            
            byte[] imageBytes = file.getBytes();
            String base64Image = Base64.getEncoder().encodeToString(imageBytes);
            teacher.setTimg(base64Image);

            teachersService.saveService(teacher);
            return new ResultData<>(200, "保存成功", null);
        } catch (Exception e) {
            return new ResultData<>(500, "保存失败：" + e.getMessage(), null);
        }
    }

    @GetMapping("/update_teacher")
    public String showTeachersPage(@RequestParam("tid") Integer tid, Model model) {
        Teachers t = teachersService.findTeaById(tid);
        model.addAttribute("t", t);
        return "updateteacher";
    }

    @PostMapping("/update-multipart")
    public ResultData<Void> updateTeachers(
            @RequestParam("tid") Integer tid,
            @RequestParam("tname") String tname,
            @RequestParam("tsex") String tsex,
            @RequestParam("tage") int tage,
            @RequestParam("ttype") int ttype,
            @RequestParam("tyear") int tyear,
            @RequestParam("tspec") String tspec,
            @RequestParam("tschool") String tschool,
            @RequestParam("tphone") String tphone,
            @RequestParam("tintr") String tintr,
            @RequestParam(value = "file", required = false) MultipartFile file,
            @RequestParam(value = "timg", required = false) String timg // 旧图片
    ) {
        try {
            Teachers teacher = new Teachers();
            teacher.setTid(tid);
            teacher.setTname(tname);
            teacher.setTsex(tsex);
            teacher.setTage(tage);
            teacher.setTtype(ttype);
            teacher.setTyear(tyear);
            teacher.setTspec(tspec);
            teacher.setTschool(tschool);
            teacher.setTphone(tphone);
            teacher.setTintr(tintr);

            if (file != null && !file.isEmpty()) {
                byte[] imageBytes = file.getBytes();
                String base64Image = Base64.getEncoder().encodeToString(imageBytes);
                teacher.setTimg(base64Image);
            } else {
                teacher.setTimg(timg);
            }

            teachersService.updateService(teacher);
            return new ResultData<>(200, "修改成功", null);
        } catch (Exception e) {
            return new ResultData<>(500, "修改失败：" + e.getMessage(), null);
        }
    }

    @PostMapping("/leave")
    @ResponseBody
    public ResultData<Void> teacherLeave(@RequestParam("tid") Integer tid) {
        teachersService.updateFlagService(tid, 0);
        return new ResultData<>(null);
    }

    @GetMapping("/detail")
    public ResultData getTeacherDetail(@RequestParam("id") Integer id) {
        Teachers teachers = teachersService.findById(id);
        if (teachers != null) {
            return new ResultData<>(teachers);
        } else {
            return new ResultData<>(null);
        }
    }

    @GetMapping("/info/{id}")
    public ResultData<Teachers> getTeacherById(@PathVariable("id") Integer id) {
        Teachers t = teachersService.findById(id);
        return new ResultData<>(t);
    }

}
